package com.nodecollege.cloud.service.impl;

import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.BindVO;
import com.nodecollege.cloud.common.model.po.UpmsTenantMenu;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.dao.mapper.UpmsTenantMenuMapper;
import com.nodecollege.cloud.service.TenantMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 绑定租户菜单信息
 *
 * @author LC
 * @date 2020/1/13 17:32
 */
@Service
public class TenantMenuServiceImpl implements TenantMenuService {

    @Autowired
    private UpmsTenantMenuMapper upmsTenantMenuMapper;

    @Override
    public void bindTenantMenu(BindVO bindVO) {
        NCUtils.nullOrEmptyThrow(bindVO.getMainSource(), new UpmsException("-1", "参数错误！"));

        // 已绑定的数据
        UpmsTenantMenu query = new UpmsTenantMenu();
        if (bindVO.getMainSource()) {
            NCUtils.nullOrEmptyThrow(bindVO.getSourceIds(), new UpmsException("-1", "绑定入参有误！"));
            query.setAppId(bindVO.getAppId());
            query.setTenantId(bindVO.getSourceIds().get(0));
        } else {
            NCUtils.nullOrEmptyThrow(bindVO.getTargetIds(), new UpmsException("-1", "绑定入参有误！"));
            query.setMenuId(bindVO.getTargetIds().get(0));
        }
        List<UpmsTenantMenu> exList = upmsTenantMenuMapper.selectListByTenantMenu(query);

        // 最终保留的数据
        List<UpmsTenantMenu> endList = new ArrayList<>();
        bindVO.getSourceIds().forEach(tenantId -> {
            bindVO.getTargetIds().forEach(menuId -> {
                UpmsTenantMenu end = new UpmsTenantMenu();
                end.setTenantId(tenantId);
                end.setAppId(bindVO.getAppId());
                end.setMenuId(menuId);
                end.setState(NCConstants.INT_1);
                endList.add(end);
            });
        });

        // 取消绑定的数据
        exList.forEach(ex -> {
            Boolean delFlag = true;
            for (int i = 0; i < endList.size(); i++) {
                if (ex.getTenantId().equals(endList.get(i).getTenantId())
                        && ex.getMenuId().equals(endList.get(i).getMenuId())) {
                    delFlag = false;
                    break;
                }
            }
            if (delFlag) {
                upmsTenantMenuMapper.deleteByPrimaryKey(ex.getTenantMenuId());
            }
        });

        // 新增绑定的数据
        endList.forEach(end -> {
            Boolean addFlag = true;
            for (int i = 0; i < exList.size(); i++) {
                if (end.getTenantId().equals(exList.get(i).getTenantId())
                        && end.getMenuId().equals(exList.get(i).getMenuId())) {
                    addFlag = false;
                    break;
                }
            }
            if (addFlag) {
                upmsTenantMenuMapper.insert(end);
            }
        });
    }

    /**
     * 锁定/解锁租户菜单状态
     *
     * @param bindVO
     */
    @Override
    public void updateTenantMenuState(BindVO bindVO) {
        NCUtils.nullOrEmptyThrow(bindVO.getSourceIds(), new UpmsException("-1", "绑定入参有误！"));
        NCUtils.nullOrEmptyThrow(bindVO.getState(), new UpmsException("-1", "锁定/解锁入参有误！"));

        // 批量进行更新
        bindVO.getSourceIds().forEach(item -> {
            UpmsTenantMenu updateAppR = new UpmsTenantMenu();
            updateAppR.setTenantMenuId(item);
            updateAppR.setState(bindVO.getState());
            upmsTenantMenuMapper.updateByPrimaryKeySelective(updateAppR);
        });
    }
}
